/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "hhdICollisionObject.h"
#include "hhdRayCastResults.h"
#include "hhdContactResults.h"
#include "hhdIDirector.h"

class btBroadphaseInterface;
class btCollisionConfiguration;
class btCollisionDispatcher;
class btConstraintSolver;
class btDiscreteDynamicsWorld;
class btCollisionObject;
class btIDebugDraw;
class btVector3;

namespace hhd
{
	namespace BulletPhysics
	{
		class CollisionShape;
		class RigidBody;

		class BulletPhysicsDirector : public IDirector
		{
			DirectorH(hhd::BulletPhysics::BulletPhysicsDirector, BulletPhysicsDirector);
			HHD_REGISTRANT_H(IDirector, BulletPhysicsDirector);

		public:
			BulletPhysicsDirector();
			~BulletPhysicsDirector();
			void initialize();
			void update(const GameTimer& in_gt);
			/**
				Returns a RayCastResults struct pointer.
				@param in_from Starting point of the ray to cast (in world space)
				@param in_to Final point of the ray to cast (in world space);
				@param group The group the ray belongs to
				@param group The mask to apply for collision filtering
				@return NULL if no results, else a pointer to data structure
			**/
			RayCastResults* rayTest(const Vector3& in_from, const Vector3& in_to, unsigned short group = 0xFFFF, unsigned short mask = 0xFFFF) const;
			ContactResults* contactTest(RigidBody*) const;
			ContactResults* contactTest(RigidBody*, unsigned short group, unsigned short mask) const;
			void setIDebugDraw(btIDebugDraw*);

			IDirector* deserializeFromXML(const TiXmlElement* in_xml_element);

			// expensive linear search of registered rigid bodies
			void getExtents(Vector3& in_mins, Vector3& in_maxs) const;
			Vector3 getGravity() const
			{
				return _gravity;
			}
		private:

			Vector3 _gravity;

			// internal bullet physics data related to the world
			btBroadphaseInterface* _broadphase;
			btCollisionConfiguration* _collision_configuration;
			btCollisionDispatcher* _dispatcher;
			btConstraintSolver* _constraint_solver;
			btDiscreteDynamicsWorld* _dynamics_world;
			btIDebugDraw* _debug_drawer;

			// this table is used to map collision objects to components so that we can send messages
			// void* is to a btCOllisionObject			
			HashTable<void*, ICollisionObject*>* _collision_object_index;
			
			IEnumerator<Pair<void*, ICollisionObject*> >* _index_enumerator;

			friend class BulletPhysicsManager;
			friend class RigidBody;
			friend class TriggerVolume;
			friend class CollisionShape;
			friend struct HogsheadRayResultCallback;
			friend struct HogsheadContactResultCallback;

			// rigidbodies, collision objects, soft bodies (someday)
			void addCollisionObject(ICollisionObject* in_body, short group=1, short mask=-1);
			void removeCollisionObject(ICollisionObject* in_body);
		};
	}
}